const _ = require('lodash')
const obj = {
  a: [
    {
      b: {
        c: 3
      }
    }
  ]
}
// const path = ['a[0]', 'b', 'c']
// const path = 'a[0].b.c'

// const a = _.get(obj, path)

// console.log(a)

function managePath(value, object) {
  let result = []
  if (Object.prototype.toString.call(value) === '[object Array]') {
    result = value
  } else if (typeof value === 'string') {
    if (value.charCodeAt(0) === 46) {
      result.push('')
    }
    const reg =
      /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g
    value.replace(reg, function (match, number, quote, subString) {
      // console.log(match, number, quote, subString)
      result.push(
        quote ? subString.replace(reEscapeChar, '$1') : number || match
      )
    })
  }
  let index = 0,
    length = result.length
  while (object != null && index < length) {
    object = object[result[index++]]
  }
  console.log(index)
  return index && index === length ? object : undefined
}

const _get = function (object, path, defaultValue) {
  let result = object === null ? undefined : managePath(path, object)
  return result === undefined ? defaultValue : result
}

// console.log(_get(obj, ['a', 0, 'b', 'c'], 'none'))
console.log(_get(obj, 'd'))
